<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends Front_Controller{	
	protected $assignData = array();
	function __construct(){
		parent::__construct();
	}
	
	/*
	 * 登陆
	 */
	public function login() {
		$post = $this->input->post();
		$account = trim(strip_tags($post['account']));
		$password = $post['password'];
		if (empty($post['account'])) {
			$this->showmsg('账号名不能为空！');
			return;
		}
		if (empty($password)) {
			$this->showmsg('密码不能为空！');
			return;
		}
		$this->load->library('SHA1', null, 'SHA1');
		$user = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('name'=>$account, 'password'=>$this->SHA1->generate($password)));
		if (empty($user)) {
			$this->showmsg('账号或者密码错误！');
			return;
		}
		$this->session->set_userdata($user[0]);
		$this->showmsg('登陆成功！', site_url('home/index'));
		return;
	}
	
	/*
	 * 注册
	 */
	function register(){
		$this->setWebTitle('注册');
		$this->load->view('user/register', $this->assignData);
		return;
	}
	
	/*
	 * 处理注册
	 */
	public function handleRegister() {
		$post = $this->input->post();
		if (empty($post['account'])) {
			$this->showmsg('账号名不能为空！');
			return;
		}
		$account = trim(strip_tags($post['account']));
		$email = $post['email'];
		//判断用户名是否存在
		$user = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('name'=>$account));
		if (!empty($user)) {
			$this->showmsg('用户名已经存在！');
			return;
		}
		if (empty($email)) {
			$this->showmsg('邮箱地址不能为空！');
			return;
		} else if (!preg_match('/^[0-9A-Za-z\.\-\_]{2,20}\@[0-9a-zA-Z\-]{1,16}\.(com|cn|org|cc|info)$/', $email)) {
			$this->showmsg('邮箱格式不对！');
			return;
		}
		//判断email是否存在
		$user = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('email'=>$email));
		if (!empty($user)) {
			$this->showmsg('邮箱地址已经存在！');
			return;
		}
		
		if (empty($post['password'])) {
			$this->showmsg('密码不能为空！');
			return;
		}
		
		if ($post['repassword'] != $post['password']) {
			$this->showmsg('重复密码跟密码不一样！');
			return;
		}
		
		if (empty($post['verify'])) {
			$this->showmsg('验证码不能为空！');
			return;
		} else {
			if ($post['verify'] != $this->session->userdata['captcha_code']) {
				$this->showmsg('验证码错误！');
				return;
			}
		}
		
		$this->load->library('SHA1', null, 'SHA1');
		$insertArr = array(
			'name'	=> $account,
			'password'	=> $this->SHA1->generate($post['password']),
			'email'		=> $email,
			'ip'		=> $this->get_client_ip(),
			'time'		=> date('Y-m-d H:i:s', time()),
		);
		$this->JayphaDB->insertDB($this->db_table_prefix.'users', $insertArr);
		$sessionArr = $this->JayphaDB->getDBResult($this->db_table_prefix.'users', array('name'=>$account));
		$this->session->set_userdata($sessionArr[0]);
		$this->showmsg('注册成功！', site_url('home/index'));
		return;
	}
	
	/*
	 * 登陆
	 */
	public function logout() {
		$this->session->sess_destroy();
		@header('Location:'.base_url());
	}
	
	//验证码
	public function captcha() {
		//显示字符数
		$length = 5;
		//内边距离
		$padding = 1;
		//宽度
		$width = 100;
		//高度
		$height = 35;
		//画布背景颜色
		$backColor = 0xFFFFFF;
		$foreColor = 0x2040A0;
		//是否透明
		$transparent = false;
		$offset = -1;
		//随即字符
		$letters = 'aeioubcdfghjklmnpqrstvwxyz';
		//cookie保存名
		$cookieName = 'captcha_code';
		$lettersLen = strlen($letters) - 1;
		$code = '';
		for($i = 0; $i < $length; ++$i)
		{
			$code.=$letters[mt_rand(0,$lettersLen)];
		}
		$image = imagecreatetruecolor($width, $height);
	
		$backColor = imagecolorallocate($image,
				(int)($backColor % 0x1000000 / 0x10000),
				(int)($backColor % 0x10000 / 0x100),
				$backColor % 0x100);
		imagefilledrectangle($image,0,0,$width, $height, $backColor);
		imagecolordeallocate($image,$backColor);

		if($transparent)
			imagecolortransparent($image,$backColor);

		$foreColor = imagecolorallocate($image, (int)($foreColor % 0x1000000 / 0x10000), (int)($foreColor % 0x10000 / 0x100),$foreColor % 0x100);
		$fontFile = BASEPATH.'../static/Duality.ttf';

		$length = strlen($code);
		//用cookie保存字符
		$this->session->set_userdata($cookieName, strtolower($code));
		$box = imagettfbbox(30,0,$fontFile,$code);
		$w = $box[4] - $box[0] + $offset * ($length - 1);
		$h = $box[1] - $box[5];
		$scale = min(($width - $padding * 2) / $w,($height - $padding * 2) / $h);
		$x = 10;
		$y = round($height * 27 / 40);
		for($i = 0; $i < $length; ++$i) {
			$fontSize = (int)(rand(27,31) * $scale * 0.9);
			$angle = rand(-10,10);
			$letter = $code[$i];
			$box = imagettftext($image,$fontSize,$angle,$x,$y,$foreColor,$fontFile,$letter);
			$x = $box[2] + $offset;
		}
		imagecolordeallocate($image,$foreColor);
		header('Pragma: public');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Content-Transfer-Encoding: binary');
		header("Content-type: image/png");
		imagepng($image);
		imagedestroy($image);
	}
}
?>